<template>
  <Dialog
    v-model="show"
    :title="$t('merchantApplicationEditDia.商家入驻申请详情')"
    width="600px"
    @close="close"
    @confirm="handleSubmit"
  >
    <div class="h-500 overflow-y-auto">
      <div class="flex lht-38" v-for="item in formattedData" :key="item.label">
        <div class="text-right w-120 color-666666 flex-shrink-0">
          {{ $t('merchantApplicationEditDia.' + item.label) }}：
        </div>
        <div v-if="item.type === 'image'" class="color-333333">
          <img :src="item.value" style="width: 200px; height: auto" />
        </div>
        <div v-else-if="item.type === 'status'" class="color-333333">
          <el-tag :type="getStatusType(item.value)">{{
            getStatusText(item.value)
          }}</el-tag>
        </div>
        <div v-else class="color-333333">{{ item.value }}</div>
      </div>

      <el-form 
        :model="formData" 
        ref="formRef" 
        :rules="rules" 
        label-width="120px"
        class="mt24"
      >
        <el-form-item :label="$t('merchantApplicationEditDia.审核结果')" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio :label="2">{{ $t('merchantApplicationEditDia.通过') }}</el-radio>
            <el-radio :label="3">{{ $t('merchantApplicationEditDia.驳回') }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item 
          :label="$t('merchantApplicationEditDia.驳回原因')" 
          prop="rejectReason"
          v-if="formData.status === 3"
        >
          <el-input 
            v-model="formData.rejectReason" 
            type="textarea" 
            rows="3" 
            :placeholder="$t('merchantApplicationEditDia.请输入驳回原因')"
          />
        </el-form-item>
      </el-form>
    </div>
  </Dialog>
</template>

<script setup>
import { ref, computed, watch } from "vue";
import Dialog from "@/components/dialog.vue";
import { useI18n } from "vue-i18n";

const { t } = useI18n();
const show = defineModel();
const emit = defineEmits(["success"]);

const props = defineProps({
  data: {
    type: Object,
    default: () => ({}),
  },
});

const formData = ref({
  status: 2,
  rejectReason: "",
});

const rules = ref({
  status: [
    { required: true, message: t('merchantApplicationEditDia.请选择审核结果'), trigger: "change" }
  ],
  rejectReason: [
    { 
      required: true, 
      message: t('merchantApplicationEditDia.请输入驳回原因'), 
      trigger: "blur",
      validator: (rule, value, callback) => {
        if (formData.value.status === 3 && !value) {
          callback(new Error(t('merchantApplicationEditDia.请输入驳回原因')))
        } else {
          callback()
        }
      }
    }
  ],
});

const formattedData = computed(() => [
  { label: "姓名", value: props.data.name },
  { label: "联系电话", value: props.data.phone },
  { label: "商家名称", value: props.data.merchantName },
  { label: "申请时间", value: props.data.applyTime },
  { label: "状态", value: props.data.status, type: "status" },
  { label: "营业执照", value: props.data.businessLicense, type: "image" },
  { label: "法人身份证", value: props.data.idCard, type: "image" },
]);

const getStatusType = (status) => {
  const map = {
    1: "warning",
    2: "success",
    3: "danger",
  };
  return map[status];
};

const getStatusText = (status) => {
  const map = {
    1: t('merchantApplicationEditDia.申请中'),
    2: t('merchantApplicationEditDia.已通过'),
    3: t('merchantApplicationEditDia.已驳回'),
  };
  return map[status];
};

watch(show, (val) => {
  if (val) {
    formData.value = {
      status: 2,
      rejectReason: "",
    };
  }
});

const formRef = ref();

const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      emit("success", {
        ...props.data,
        ...formData.value
      });
      close();
    }
  });
};

const close = () => {
  formData.value = {
    status: 2,
    rejectReason: "",
  };
  show.value = false;
};
</script>

<style lang="scss" scoped>
.h-500 {
  height: 500px;
}
.lht-38 {
  line-height: 38px;
}
.w-120 {
  width: 120px;
}
.mt24 {
  margin-top: 24px;
}
</style>
